from django.db import models
from django.contrib.auth.models import User
import datetime

# Create your models here.

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    activation_key = models.CharField(max_length=40)
    key_expires = models.DateTimeField()


class Category(models.Model):
	name = models.CharField(max_length=50)
	description = models.CharField(max_length=100)
	created = models.DateField(editable=False, blank=True, null=True)
	updated = models.DateField(editable=False, blank=True, null=True)
	
	def __str__(self):
		return self.name
	
	def save(self):
		if not self.id:
			self.created = datetime.datetime.today()
		self.updated = datetime.datetime.today()
		super(Category, self).save()
	

class UserCode(models.Model):
	title = models.CharField(max_length=50)
	description = models.CharField(max_length=100)
	code = models.TextField(max_length=500)
	created = models.DateField(editable=False, blank=True, null=True)
	updated = models.DateField(editable=False, blank=True, null=True)
	user = models.ForeignKey(User, related_name="codes") 
	category = models.ForeignKey(Category, related_name="codes")
	# public security
	readable = models.BooleanField(True)
	writable = models.BooleanField(True)
	def __str__(self):
		return self.title
	
	def save(self):
		if not self.id:
			self.created = datetime.datetime.today()
		self.updated = datetime.datetime.today()
		super(UserCode, self).save()


